// SPDX-FileCopyrightText: Copyright 2025-2025 深圳市同心圆网络有限公司
// SPDX-License-Identifier: Apache-2.0

package roadmap_member_api_serv

import (
	"context"
	"strings"
	"time"

	"gitcode.com/lvupclub/lvup-api/dao"
	"gitcode.com/lvupclub/lvup-api/dao/roadmap_dao"
	"gitcode.com/lvupclub/proto-gen-go.git/src/roadmap_member_api"
	"github.com/dchest/uniuri"
	"github.com/google/uuid"
)

func (apiImpl *RoadmapMemberApiImpl) GenInvite(ctx context.Context, req *roadmap_member_api.GenInviteRequest) (*roadmap_member_api.GenInviteResponse, error) {
	sessItem, ok := dao.CacheDao.GetSession(ctx, req.SessionId)
	if !ok {
		return &roadmap_member_api.GenInviteResponse{
			Code:   roadmap_member_api.GenInviteResponse_CODE_WRONG_SESSION,
			ErrMsg: "会话不存在",
		}, nil
	}
	roadmapItem, err := roadmap_dao.RoadmapInfoDao.Get(ctx, req.RoadmapId)
	if err != nil {
		return &roadmap_member_api.GenInviteResponse{
			Code:   roadmap_member_api.GenInviteResponse_CODE_NO_ROADMAP,
			ErrMsg: "成长路线图不存在",
		}, nil
	}
	if roadmapItem.OwnerUserId != sessItem.UserId {
		return &roadmap_member_api.GenInviteResponse{
			Code:   roadmap_member_api.GenInviteResponse_CODE_NO_PERMISSION,
			ErrMsg: "没有权限",
		}, nil
	}

	inviteCode := uniuri.NewLenChars(32, []byte("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")) +
		strings.ReplaceAll(uuid.NewString(), "-", "")
	nowTime := time.Now().UnixMilli()

	dbSess, err := dao.StartSession()
	if err != nil {
		return nil, err
	}
	defer dao.EndSession(dbSess)

	ret, err := dbSess.WithTransaction(ctx, func(sessCtx context.Context) (interface{}, error) {
		err = roadmap_dao.InviteInfoDao.Insert(sessCtx, &roadmap_dao.InviteInfoDbItem{
			InviteCode: inviteCode,
			RoadmapId:  req.RoadmapId,
			CreateTime: nowTime,
			ExpireTime: req.ExpireTime,
		})
		if err != nil {
			return nil, err
		}
		return &roadmap_member_api.GenInviteResponse{
			Code:       roadmap_member_api.GenInviteResponse_CODE_OK,
			InviteCode: inviteCode,
		}, nil
	})
	if err != nil {
		return nil, err
	}
	return ret.(*roadmap_member_api.GenInviteResponse), nil
}

func (apiImpl *RoadmapMemberApiImpl) ListInvite(ctx context.Context, req *roadmap_member_api.ListInviteRequest) (*roadmap_member_api.ListInviteResponse, error) {
	emptyList := []*roadmap_member_api.InviteInfo{}

	sessItem, ok := dao.CacheDao.GetSession(ctx, req.SessionId)
	if !ok {
		return &roadmap_member_api.ListInviteResponse{
			Code:       roadmap_member_api.ListInviteResponse_CODE_WRONG_SESSION,
			ErrMsg:     "会话不存在",
			InviteList: emptyList,
		}, nil
	}
	roadmapItem, err := roadmap_dao.RoadmapInfoDao.Get(ctx, req.RoadmapId)
	if err != nil {
		return &roadmap_member_api.ListInviteResponse{
			Code:       roadmap_member_api.ListInviteResponse_CODE_NO_ROADMAP,
			ErrMsg:     "成长路线图不存在",
			InviteList: emptyList,
		}, nil
	}
	if roadmapItem.OwnerUserId != sessItem.UserId {
		return &roadmap_member_api.ListInviteResponse{
			Code:       roadmap_member_api.ListInviteResponse_CODE_NO_PERMISSION,
			ErrMsg:     "没有权限",
			InviteList: emptyList,
		}, nil
	}

	inviteItemList, err := roadmap_dao.InviteInfoDao.List(ctx, req.RoadmapId)
	if err != nil {
		return nil, err
	}
	inviteList := []*roadmap_member_api.InviteInfo{}
	for _, inviteItem := range inviteItemList {
		inviteList = append(inviteList, inviteItem.ToInviteInfo())
	}
	return &roadmap_member_api.ListInviteResponse{
		Code:       roadmap_member_api.ListInviteResponse_CODE_OK,
		InviteList: inviteList,
	}, nil
}

func (apiImpl *RoadmapMemberApiImpl) RemoveInvite(ctx context.Context, req *roadmap_member_api.RemoveInviteRequest) (*roadmap_member_api.RemoveInviteResponse, error) {
	sessItem, ok := dao.CacheDao.GetSession(ctx, req.SessionId)
	if !ok {
		return &roadmap_member_api.RemoveInviteResponse{
			Code:   roadmap_member_api.RemoveInviteResponse_CODE_WRONG_SESSION,
			ErrMsg: "会话不存在",
		}, nil
	}
	roadmapItem, err := roadmap_dao.RoadmapInfoDao.Get(ctx, req.RoadmapId)
	if err != nil {
		return &roadmap_member_api.RemoveInviteResponse{
			Code:   roadmap_member_api.RemoveInviteResponse_CODE_NO_ROADMAP,
			ErrMsg: "成长路线图不存在",
		}, nil
	}
	if roadmapItem.OwnerUserId != sessItem.UserId {
		return &roadmap_member_api.RemoveInviteResponse{
			Code:   roadmap_member_api.RemoveInviteResponse_CODE_NO_PERMISSION,
			ErrMsg: "没有权限",
		}, nil
	}
	exist, err := roadmap_dao.InviteInfoDao.Exist(ctx, req.InviteCode)
	if err != nil {
		return nil, err
	}
	if !exist {
		return &roadmap_member_api.RemoveInviteResponse{
			Code: roadmap_member_api.RemoveInviteResponse_CODE_OK,
		}, nil
	}

	dbSess, err := dao.StartSession()
	if err != nil {
		return nil, err
	}
	defer dao.EndSession(dbSess)

	ret, err := dbSess.WithTransaction(ctx, func(sessCtx context.Context) (interface{}, error) {
		err = roadmap_dao.InviteInfoDao.Remove(ctx, req.InviteCode, req.RoadmapId)
		if err != nil {
			return nil, err
		}
		return &roadmap_member_api.RemoveInviteResponse{
			Code: roadmap_member_api.RemoveInviteResponse_CODE_OK,
		}, nil
	})
	if err != nil {
		return nil, err
	}
	return ret.(*roadmap_member_api.RemoveInviteResponse), nil
}
